﻿/*
#-------------------------------------------------------------------------------
# Name:        XMLReader - Reads Testsamples xml format
#
# Usage:       Pass on the filename to ReadXML to read the XML File
#              The file has to have the Testsamples format
#
# Requires:    Visual Studio 2010 and up.
#
# Author:      Kasper van Voorst
# 
# Modified by: Roy Scheefhals
#  05-04-2012  changes: Changed the full ReadXml to the XMLProvider principle.
#                       (Commented Code is made by Kasper van Voorst)
#
# Created:     03-04-2012
#-------------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

namespace LibraryCS
{
    public class XMLreader
    {
        public XMLreader()
        {
        }
        public List<LicensePlate> readXML(string filename)
        {
            List<Point> upperLeftCoords = new List<Point>();
            List<Point> upperRightCoords = new List<Point>();
            List<Point> bottomLeftCoords = new List<Point>();
            List<Point> bottomRightCoords = new List<Point>();
            
            List<LicensePlate> lPlates = new List<LicensePlate>();
            //Load the XML file into a DataSet, a data structure with rows, columns and tables much like an XML document has; this makes it easy to extract the data we need.
            //DataSet ds = new DataSet();

            XMLProvider XMLDocument;
            try
            {
                XMLDocument = new XMLProvider(new FileInfo(filename));
            }
            catch (FileNotFoundException e)
            {
                MessageBox.Show("Error when reading XML file, check the format of this file\n" + e.Message, "XML error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return lPlates;
            }
            catch (Exception e)
            {
                MessageBox.Show("Error when reading XML file, check the existance /  of this file\n" + e.Message, "XML error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return lPlates;
            }
            LicensePlate LP = new LicensePlate();
             
            XmlNode CurNode = XMLDocument.getFirstNode();
            string s = CurNode.InnerText;
            string toputin = "";
            foreach (char c in s)
            {
                if (c != '-')
                {
                    toputin = toputin + c;
                }
            }
            LP.text = toputin;
            XmlNode ImageNodeAttribute = CurNode.Attributes[0];
            if (ImageNodeAttribute.Name.ToLower() == "filename")
                LP.filename = ImageNodeAttribute.Value;

            XMLDocument.nodeLayerDown();
            XMLDocument.nodeLayerDown();

            int amountofChilds = XMLDocument.activeNode.ChildNodes.Count;

            for (int i = 0; i < amountofChilds; i++)
            {
                XmlNode childnode = XMLDocument.activeNode.ChildNodes[i];
                XmlNode XmlAttribute1 = childnode.Attributes[0];
                XmlNode XmlAttribute2 = childnode.Attributes[1];
                if (childnode.Name.ToLower() == "upperleft")
                {
                    if (XmlAttribute1.Name.ToLower() == "x")
                    {
                        LP.topleft.X = Int32.Parse(XmlAttribute1.Value);
                        LP.topleft.Y = Int32.Parse(XmlAttribute2.Value);
                    }
                    else
                    {
                        LP.topleft.X = Int32.Parse(XmlAttribute2.Value);
                        LP.topleft.Y = Int32.Parse(XmlAttribute1.Value);
                    }
                }
                else if (childnode.Name.ToLower() == "upperright")
                {
                    if (XmlAttribute1.Name.ToLower() == "x")
                    {
                        LP.topright.X = Int32.Parse(XmlAttribute1.Value);
                        LP.topright.Y = Int32.Parse(XmlAttribute2.Value);
                    }
                    else
                    {
                        LP.topright.X = Int32.Parse(XmlAttribute2.Value);
                        LP.topright.Y = Int32.Parse(XmlAttribute1.Value);
                    }
                }
                else if (childnode.Name.ToLower() == "lowerleft")
                {
                    if (XmlAttribute1.Name.ToLower() == "x")
                    {
                        LP.bottomleft.X = Int32.Parse(XmlAttribute1.Value);
                        LP.bottomleft.Y = Int32.Parse(XmlAttribute2.Value);
                    }
                    else
                    {
                        LP.bottomleft.X = Int32.Parse(XmlAttribute2.Value);
                        LP.bottomleft.Y = Int32.Parse(XmlAttribute1.Value);
                    }
                }
                else if (childnode.Name.ToLower() == "lowerright")
                {
                    if (XmlAttribute1.Name.ToLower() == "x")
                    {
                        LP.bottomright.X = Int32.Parse(XmlAttribute1.Value);
                        LP.bottomright.Y = Int32.Parse(XmlAttribute2.Value);
                    }
                    else
                    {
                        LP.bottomright.X = Int32.Parse(XmlAttribute2.Value);
                        LP.bottomright.Y = Int32.Parse(XmlAttribute1.Value);
                    }
                }
            }
            LP.topwidth = LP.topright.X - LP.topleft.X;
            LP.leftheight = LP.bottomleft.Y - LP.topleft.Y;
            LP.bottomwidth = LP.bottomright.X - LP.bottomleft.X;
            LP.rightheigth = LP.bottomright.Y - LP.topright.Y;
            lPlates.Add(LP);

            XMLDocument.nodeLayerUp();
            XMLDocument.nodeLayerUp();

            while (XMLDocument.nextSibling())
            {
                LP = new LicensePlate();
                CurNode = XMLDocument.activeNode; 
                s = CurNode.InnerText;
                toputin = "";
                foreach (char c in s)
                {
                    if (c != '-')
                    {
                        toputin = toputin + c;
                    }
                }
                LP.text = toputin;
                ImageNodeAttribute = CurNode.Attributes[0];
                if (ImageNodeAttribute.Name.ToLower() == "filename")
                    LP.filename = ImageNodeAttribute.Value;

                XMLDocument.nodeLayerDown();
                XMLDocument.nodeLayerDown();

                amountofChilds = XMLDocument.activeNode.ChildNodes.Count;

                for (int i = 0; i < amountofChilds; i++)
                {
                    XmlNode childnode = XMLDocument.activeNode.ChildNodes[i];
                    XmlNode XmlAttribute1 = childnode.Attributes[0];
                    XmlNode XmlAttribute2 = childnode.Attributes[1];
                    if (childnode.Name.ToLower() == "upperleft")
                    {
                        if (XmlAttribute1.Name.ToLower() == "x")
                        {
                            LP.topleft.X = Int32.Parse(XmlAttribute1.Value);
                            LP.topleft.Y = Int32.Parse(XmlAttribute2.Value);
                        }
                        else
                        {
                            LP.topleft.X = Int32.Parse(XmlAttribute2.Value);
                            LP.topleft.Y = Int32.Parse(XmlAttribute1.Value);
                        }
                    }
                    else if (childnode.Name.ToLower() == "upperright")
                    {
                        if (XmlAttribute1.Name.ToLower() == "x")
                        {
                            LP.topright.X = Int32.Parse(XmlAttribute1.Value);
                            LP.topright.Y = Int32.Parse(XmlAttribute2.Value);
                        }
                        else
                        {
                            LP.topright.X = Int32.Parse(XmlAttribute2.Value);
                            LP.topright.Y = Int32.Parse(XmlAttribute1.Value);
                        }
                    }
                    else if (childnode.Name.ToLower() == "lowerleft")
                    {
                        if (XmlAttribute1.Name.ToLower() == "x")
                        {
                            LP.bottomleft.X = Int32.Parse(XmlAttribute1.Value);
                            LP.bottomleft.Y = Int32.Parse(XmlAttribute2.Value);
                        }
                        else
                        {
                            LP.bottomleft.X = Int32.Parse(XmlAttribute2.Value);
                            LP.bottomleft.Y = Int32.Parse(XmlAttribute1.Value);
                        }
                    }
                    else if (childnode.Name.ToLower() == "lowerright")
                    {
                        if (XmlAttribute1.Name.ToLower() == "x")
                        {
                            LP.bottomright.X = Int32.Parse(XmlAttribute1.Value);
                            LP.bottomright.Y = Int32.Parse(XmlAttribute2.Value);
                        }
                        else
                        {
                            LP.bottomright.X = Int32.Parse(XmlAttribute2.Value);
                            LP.bottomright.Y = Int32.Parse(XmlAttribute1.Value);
                        }
                    }
                }

                LP.topwidth = LP.topright.X - LP.topleft.X;
                LP.leftheight = LP.bottomleft.Y - LP.topleft.Y;
                LP.bottomwidth = LP.bottomright.X - LP.bottomleft.X;
                LP.rightheigth = LP.bottomright.Y - LP.topright.Y;
                lPlates.Add(LP);

                XMLDocument.nodeLayerUp();
                XMLDocument.nodeLayerUp();
            }

            /*List<string> textset = new List<string>();
            List<string> filenames = new List<string>();
            //Provide the indexes of the XML tables we need and load the data from them into usable lists      
            foreach (DataRow dr in ds.Tables["license_plate"].Rows)
            {
                string s = (string)dr["text"];
                string toputin = "";
                foreach (char c in s)
                {
                    if (c != '-')
                    {
                        toputin = toputin + c;                       
                    }               
                }
                textset.Add(toputin);
            }

            foreach (DataTable dt in ds.Tables)
            {
                dt.TableName.ToLower();
            }
            foreach (DataRow dr in ds.Tables["upperleft"].Rows)
            {
                int x = int.Parse((string)dr["x"]);
                int y = int.Parse((string)dr["y"]);
                upperLeftCoords.Add(new Point(x, y));
            }

            foreach (DataRow dr in ds.Tables["lowerright"].Rows)
            {
                int x = int.Parse((string)dr["x"]);
                int y = int.Parse((string)dr["y"]);
                bottomRightCoords.Add(new Point(x, y));
            }
            foreach (DataRow dr in ds.Tables["lowerleft"].Rows)
            {
                int x = int.Parse((string)dr["x"]);
                int y = int.Parse((string)dr["y"]);
                bottomLeftCoords.Add(new Point(x, y));
            }
            foreach (DataRow dr in ds.Tables["upperright"].Rows)
            {
                int x = int.Parse((string)dr["x"]);
                int y = int.Parse((string)dr["y"]);
                upperRightCoords.Add(new Point(x, y));
            }
            foreach (DataRow dr in ds.Tables["image"].Rows)
            {
                filenames.Add((string)dr["filename"]);
            }
            //Lastly, the lists for the Points and the corresponding license plate texts are loaded into data objects.

            List<LicensePlate> lPlates = new List<LicensePlate>();

            for (int q = 0; q < upperLeftCoords.Count; q++)
            {
                lPlates.Add(new LicensePlate(upperLeftCoords[q], bottomLeftCoords[q], upperRightCoords[q], bottomRightCoords[q], textset[q], filenames[q]));
            }*/
            
            return lPlates;
        }

        //by kasper van Voorst
        public string[] compareLicensePlates(List<LicensePlate> programResults, string filename)
        {
            List<LicensePlate> xmlPlates = readXML(filename);
            string[] s = new string[programResults.Count];
            foreach (LicensePlate lp in programResults)
            {
                int x = programResults.IndexOf(lp);
                int correct = lp.compareText(programResults[x]);
                double locationCorrectness = lp.compareLocation(programResults[x]);
                string str = "Characters read successfully: " + correct + ", location correctness: " + locationCorrectness + "%";
                s[x] = str;
            }
            return s;
        }
    }
}
